import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
      name: 'sort'
})
export class SortPipe implements PipeTransform {

      transform(tbody: any[], [SortBy, Dir]: any) {
            // tbody异步获取，管道执行的时候tbody没有给默认数组  会抛异常
            try {
                  tbody.sort((a: any, b: any) => {
                        if (a[SortBy] > b[SortBy]) {
                              return 1 * Dir
                        }
                        if (a[SortBy] < b[SortBy]) {
                              return -1 * Dir;
                        }
                        return 0;
                  });
            } catch (e) {
                  console.log(e);
            }
            return tbody;
      }

}
